const router = require('koa-router')()
const fs = require('fs');
const path = require("path");
// const imgPath = path.join(__dirname, '../../public/images');
const imgPath = path.join(path.resolve(__dirname, '../../..'), '/public/images');

console.log(imgPath, 'at index.js');

router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})

router.get('/image/avatars/:id', async (ctx, next) => {
  console.log(imgPath);
  console.log(path.join(imgPath, '/avatars/' + ctx.params.id));
  console.log(ctx.path.split('/')[2]);
  console.log(ctx.params.id);
  let obj = fs.readFileSync(path.join(imgPath, '/avatars/' + ctx.params.id));
  console.log(obj);
  ctx.body = obj;
})

router.get('/image/courseimg/:id', async (ctx, next) => {
  console.log(imgPath);
  console.log(path.join(imgPath, '/course_imgs/' + ctx.params.id));
  console.log(ctx.path.split('/')[2]);
  console.log(ctx.params.id);
  let obj = fs.readFileSync(path.join(imgPath, '/course_imgs/' + ctx.params.id));
  console.log(obj);
  ctx.body = obj;
})

module.exports = router
